package com.shenmazong.servertalkstudent.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.pojo.TbStudent;
import com.shenmazong.servertalkstudent.mapper.TbStudentMapper;
import com.shenmazong.servertalkstudent.service.TbStudentService;
import com.shenmazong.utils.ResponseResult;
import com.shenmazong.vo.StudentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;

/**
 * @Author 王大勇
 * @Date 2020-9-16
 **/
@Service
@Slf4j
public class TbStudentServiceImpl implements TbStudentService {

    @Autowired
    private TbStudentMapper studentMapper;

    @Override
    public ResponseResult setUserPass(Integer userId, String oldPass, String newPass) {

        ResponseResult result=ResponseResult.SUCCESS();
        //判断学生是否存在
        TbStudent student = studentMapper.selectOne(
                new QueryWrapper<TbStudent>()
                        .eq("id", userId)
                        .eq("status", 0)
                        .last("limit 1")
        );
        if (student==null){
            result.setFailMessage("学生不存在");
            return  result;
        }

        String oldPassWord=oldPass+"."+student.getPassSalt();
        //加密
        String oldMd5Password= DigestUtils.md5DigestAsHex(oldPassWord.getBytes());

        //判断旧密码是否正确
        if (!student.getUserPass().equals(oldMd5Password)) {
            result.setFailMessage("密码不正确");
            return  result;
        }

        //加盐
        String password=newPass+"."+student.getUserPass();
        //加密
        String md5Password= DigestUtils.md5DigestAsHex(password.getBytes());
        student.setUserPass(md5Password);
        log.info("pass:"+md5Password);
        //修改密码
        studentMapper.updateById(student);
        student.setUserPass("");
        student.setPassSalt("");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("student",student);
        result.setResult(hashMap);
        return result;
    }

    @Override
    public ResponseResult getClassByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();

        //判断学生是否存在
        TbStudent student = studentMapper.selectOne(
                new QueryWrapper<TbStudent>()
                        .eq("id", userId)
                        .eq("status", 0)
                        .last("limit 1")
        );
        if (student==null){
            result.setFailMessage("学生不存在");
            return  result;
        }
        //查询分页
        Page<StudentVo> page = new Page<>(pageCurrent, pageSize);
        IPage<StudentVo> iPage = studentMapper.getClassByPage(page, userId);

        HashMap<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("students",iPage);
        result.setResult(hashMap);

        //iPage.getPages()
        //iPage.getRecords()
        //iPage.getTotal()

       return result;
    }

    @Override
    public ResponseResult getFinishClassByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        //判断学生是否存在
        TbStudent student = studentMapper.selectOne(
                new QueryWrapper<TbStudent>()
                        .eq("id", userId)
                        .eq("status", 0)
                        .last("limit 1")
        );
        if (student==null){
            result.setFailMessage("学生不存在");
            return  result;
        }
        //查询分页
        Page<StudentVo> page = new Page<StudentVo>(pageCurrent, pageSize);
        IPage<StudentVo> pages = studentMapper.getFinishClassByPage(page, userId);

        HashMap<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("students",pages);
        result.setResult(hashMap);

        return result;
    }

    @Override
    public ResponseResult getWillClassByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        TbStudent student = studentMapper.selectOne(
                new QueryWrapper<TbStudent>()
                        .eq("id", userId)
                        .eq("status", 0)
                        .last("limit 1")
        );
        if (student==null){
            result.setFailMessage("学生不存在");
            return  result;
        }
        //查询分页
        Page<StudentVo> page = new Page<StudentVo>(pageCurrent, pageSize);
        IPage<StudentVo> pages = studentMapper.getWillClassByPage(page, userId);

        HashMap<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("students",pages);
        result.setResult(hashMap);
        return result;
    }

    @Override
    public ResponseResult getPayFeeByPage(Integer userId, Integer pageCurrent, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        //判断学生是否存在
        TbStudent student = studentMapper.selectOne(
                new QueryWrapper<TbStudent>()
                        .eq("id", userId)
                        .eq("status", 0)
                        .last("limit 1")
        );
        if (student==null){
            result.setFailMessage("学生不存在");
            return  result;
        }
        Page<StudentVo> page = new Page<StudentVo>(pageCurrent, pageSize);
        IPage<StudentVo> pages = studentMapper.getClassByPage(page, userId);

        double sumFee=0;
        for (StudentVo s:pages.getRecords()
             ) {
            sumFee+=s.getClassFee();
        }
        HashMap<String, Object> hashMap = new HashMap<>(10);
        hashMap.put("students",pages);
        hashMap.put("sumFee",sumFee);
        log.info("sumFee"+sumFee);
        result.setResult(hashMap);
        return result;
    }
}
